//
// Created by Administrator on 2024/9/9.
//

#include "aikuaiApi.h"

String getLoadCookie(String ip, String user, String pwd)
{
    String get_cookie;
    const char *headerKeys[] = {"Set-Cookie"};
    HTTPClient http;
    http.begin(ip);

    // Stream& output;

    unsigned char *hash = MD5::make_hash((char *) pwd.c_str());
    //generate the digest (hex encoding) of our hash
    char *md5str = MD5::make_digest(hash, 16);
    free(hash);
    String md5 = md5str;
    //print it on our serial monitor
    Serial.println(md5);
    //Give the Memory back to the System if you run the md5 Hash generation in a loop
    free(md5str);

    // Stream& output;
    String req_json;
    JsonDocument doc;

    doc["username"] = user;
    doc["passwd"] = md5;

    doc.shrinkToFit();  // optional

    serializeJson(doc, req_json);
    Serial.println(req_json);
//    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//    const char *data = "{\r\n    \"username\": \"test\",\r\n    \"passwd\": \"cc03e747a6afbbcbf8be7668acfebee5\"\r\n}";
//    const char *data = "{\r\n  \"func_name\": \"homepage\",\r\n  \"action\": \"show\",\r\n  \"param\": {\r\n    \"TYPE\": \"sysstat\"\r\n  }\r\n}";
// 构造请求体
    http.collectHeaders(headerKeys, 1); // 准备需要接收的响应头内容
//    http.addHeader("Cookie", "sess_key=4893970a7617a8b876e1e97929ada169; path=/;");
// 发送HTTP-POST请求
    int httpResponseCode = http.POST(req_json);
    Serial.println(http.headers());
// 处理服务器的响应

    if (httpResponseCode > 0)
    {
        String response = http.getString();
        get_cookie = http.header("Set-Cookie");
        Serial.println(get_cookie);
    } else
    {
        Serial.println("Error sending POST request");
    }
    http.end();
    return get_cookie;
}

sysStater getSysRunStater(String cookie)
{
    sysStater st;
    st.valid_data = false;
//    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
//    const char *data = "{\r\n    \"username\": \"test\",\r\n    \"passwd\": \"cc03e747a6afbbcbf8be7668acfebee5\"\r\n}";
    const char *data = "{\r\n  \"func_name\": \"homepage\",\r\n  \"action\": \"show\",\r\n  \"param\": {\r\n    \"TYPE\": \"sysstat,ac_status\"\r\n  }\r\n}";
// 构造请求体
//    http.addHeader("Cookie", "sess_key=4893970a7617a8b876e1e97929ada169; path=/;");
// 发送HTTP-POST请求
    HTTPClient http;
    http.begin("http://yu.vip7x24.com:81/Action/call");
    http.addHeader("Cookie", cookie);

    int httpResponseCode = http.POST(data);
// 处理服务器的响应

    if (httpResponseCode > 0)
    {
        String response = http.getString();
        ESP_LOGD("re", "%s", response.c_str());
        JsonDocument doc;

        DeserializationError error = deserializeJson(doc, response);

        if (error)
        {
            Serial.print("deserializeJson() failed: ");
            Serial.println(error.c_str());
            st.err_code = 0;
            return st;
        }

        int Result = doc["Result"]; // 30000
        String ErrMsg = doc["ErrMsg"]; // "Success"

        if (ErrMsg.indexOf("Success") == -1)
        {
            st.err_code = 1;
            return st;
        }

        JsonObject Data_sysstat = doc["Data"]["sysstat"];

        JsonArray Data_sysstat_cpu = Data_sysstat["cpu"];
        {
            String t = Data_sysstat_cpu[0]; // "0.50%"
            st.CPU_user = t.toInt();
            Serial.println(st.CPU_user);
            Serial.println(t);
        }

        st.CPU_temp = (uint8_t)Data_sysstat["cputemp"][0]; // 43

        st.link_status = Data_sysstat["link_status"]; // 0

        JsonObject Data_sysstat_memory = Data_sysstat["memory"];
//        st.memory_user = // "27%"
        {
            String t =  Data_sysstat_memory["used"]; // "0.50%"
            st.memory_user = t.toInt();
        }
        JsonObject Data_sysstat_online_user = Data_sysstat["online_user"];
        st.connect_num = Data_sysstat_online_user["count"]; // 71
        st.online_2_4G = Data_sysstat_online_user["count_2g"]; // 0
        st.online_5G = Data_sysstat_online_user["count_5g"]; // 0
        st.count_wired = Data_sysstat_online_user["count_wired"]; // 71
        st.count_wired_less = Data_sysstat_online_user["count_wireless"]; // 0

        JsonObject Data_sysstat_stream = Data_sysstat["stream"];
        st.connect_num = Data_sysstat_stream["connect_num"]; // 217
        st.upload = Data_sysstat_stream["upload"]; // 284334
        st.down_load = Data_sysstat_stream["download"]; // 3202

        st.sys_run_day = Data_sysstat["uptime"]; // 577405
        Serial.println(st.sys_run_day);

        st.ap_count = doc["Data"]["ac_status"]["ap_count"]; // 0
        st.ap_online = doc["Data"]["ac_status"]["ap_online"]; // 0

    } else
    {
        Serial.println("Error sending POST request");
    }
    http.end();
    st.err_code = 200;
    return st;
}